package Thread;

public class PhoneCase {
    private boolean isTake=false;//用来判断用户机的状态

    public static void main(String[] args) {
        new PhoneCase().open();//开机

    }
    void open(){
        Thread call = new Thread(() -> {
              while (true){
                  try {
                      if(!isTake){
                      synchronized (PhoneCase.this) {//出于匿名内部类中 不能直接this
                          System.out.println("等待用户来电中...");
                          Thread.sleep(3000);
                          PhoneCase.this.notify();
                          System.out.println("用户来电了...");
                          isTake = true;
                          PhoneCase.this.wait();
                        }
                      }else {
                          PhoneCase.this.notify();
                          PhoneCase.this.wait();
                      }
                  } catch (Exception e) {
                      e.printStackTrace();
                  }

              }
        });
        Thread take = new Thread(() -> {

            while (true) {
                try {
                    synchronized (PhoneCase.this) {
                        if (isTake){
                            System.out.println("接听中...");
                            Thread.sleep(5000);
                            PhoneCase.this.notify();
                            System.out.println("接听完毕...");
                            isTake=false;
                            PhoneCase.this.wait();
                        }/*else {
                            System.out.println("闯入...");
                        }*/
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        call.start();
        take.start();
    }
}
